/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package JKDateAndTime;

import javax.swing.JOptionPane;
import java.io.IOException;
import java.util.Calendar;

/**
 * 
 * Class that contains methods to getting and setting the date and time.
 * 
 * @author Royce Feng
 */
public class DateAndTimeClass implements Runnable {
    
    private DateAndTimeGUI parent;
    private Calendar cal;
    
    /**
     * 
     * Main constructor for DateAndTimeClass, creates a new calendar instance 
     * as well as sets the parent variable
     * 
     * @param _parent Used to send data back to the parent
     */
    public DateAndTimeClass( DateAndTimeGUI _parent ) {
        parent = _parent;
        cal = Calendar.getInstance();
    }
    
    /**
     * 
     * Sets the date and time using windows CMD.
     * 
     * @param y Year
     * @param m Month
     * @param d Day
     * @param hour Hour
     * @param minute Minute
     * @param second Second
     * @return True if date and time are correctly set, false if they are not.
     */
    public static boolean setDateAndTime( int y, int m, int d, int hour, int minute, int second ) {
        String osName = System.getProperty("os.name");
        if (osName.toUpperCase().startsWith(("WINDOWS"))) {
            try {
                String datestr = m + "-" + d + "-" + y;
                String timestr = hour + ":" + minute + ":" + second;
                Runtime.getRuntime().exec("cmd /C date " + datestr);
                Runtime.getRuntime().exec("cmd /C time " + timestr);
                return true;
            } catch (IOException e) {
                return false;
            }
        } else {
            JOptionPane.showMessageDialog(null, "Current Operating System is not windows", "Unrecognized Operating System", JOptionPane.ERROR_MESSAGE);
            return false;
        }
    }
    
    /**
     * 
     * Runnable procedure used to get the current time.
     * 
     */
    public void run() {
        cal = Calendar.getInstance();
        parent.setTime( cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 
            cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND) );
    }

}
